package personnel.department.pages;


import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

import personnel.department.pages.constants.Icons;

public class MainPage {

	private static final int LEFT_COMPOSITE_WIDTH = 309;
	private Shell shell;
	private Composite leftComposite;
	private Composite rightComposite;
	private PagesLogic pagesLogic;
	private Image image;

	public MainPage(Shell shell) {
		this.shell = shell;
		this.pagesLogic = PagesLogic.getInstance();
		pagesLogic.setMainShell(shell);
		image = new Image(getDisplay(), Icons.FON_ICON.getPath());
		createContent();
	}
	
	private void createContent() {
		initShell();
		createLeftComposite();
		
		createTree();
		createResultComposite();
		createInfoComposite();
		
		createRightComposite();
		
	}

	private void createResultComposite() {
		QueryResultComposite resultComposite = new QueryResultComposite(leftComposite, SWT.BORDER);
		pagesLogic.setResultComposite(resultComposite);
	}

	private void createInfoComposite() {
		EmployeeInfoComposite infoComposite = new EmployeeInfoComposite(leftComposite, SWT.BORDER);
		pagesLogic.setInfoComposite(infoComposite);
	}

	private void createTree() {
		PersonnelTree tree = new PersonnelTree(shell, leftComposite, rightComposite);
		tree.createTree();
		pagesLogic.setTree(tree.getTree());
	}

	private void createLeftComposite() {
		leftComposite = new Composite(shell, SWT.BORDER);
		leftComposite.setBackground(JFaceColors.getBannerBackground(getDisplay()));
		GridData gridData = new GridData(SWT.NONE, SWT.FILL, false, true);
		gridData.widthHint = LEFT_COMPOSITE_WIDTH;
		leftComposite.setLayoutData(gridData);
		GridLayoutFactory.fillDefaults().spacing(0, 5).applyTo(leftComposite);
	}
	
	private void createRightComposite() {
		//Creating tabFolder move to PageLogic
		shell.addPaintListener(e -> e.gc.drawImage(image, (e.width + LEFT_COMPOSITE_WIDTH)/2 - image.getImageData().width/2, e.height/2 - image.getImageData().height/2));
	}

	private void initShell() {
		shell.setBackground(new Color(getDisplay(), new RGB(255, 255, 255)));
		shell.setLayout(new GridLayout(2, false));
	}

	private Display getDisplay() {
		return shell.getDisplay();
	}

	public Composite getLeftComposite() {
		return leftComposite;
	}

	public Composite getRightComposite() {
		return rightComposite;
	}
	
}
